package com.cardone.platform.authority.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.authority.dto.PeVaRuTransactModeDto;

/**
 * 许可、验证规则与处理模式
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class PeVaRuTransactModeJdbcDao implements PeVaRuTransactModeDao {
	@Override
	public int deleteByIds(final PeVaRuTransactModeDto deletePeVaRuTransactMode) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deletePeVaRuTransactMode.getAttrs())) {
			model.putAll(deletePeVaRuTransactMode.getAttrs());
		}

		model.put(Attributes.ids.name(), deletePeVaRuTransactMode.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final PeVaRuTransactModeDto findPeVaRuTransactMode) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findPeVaRuTransactMode, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findPeVaRuTransactMode, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findPeVaRuTransactMode.getAttrs())) {
			model.putAll(findPeVaRuTransactMode.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final PeVaRuTransactModeDto findPeVaRuTransactMode) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findPeVaRuTransactMode);

		final Model model = ModelUtils.newModel(new ModelArgs(findPeVaRuTransactMode, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findPeVaRuTransactMode.getAttrs())) {
			model.putAll(findPeVaRuTransactMode.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final PeVaRuTransactModeDto findListPeVaRuTransactMode) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListPeVaRuTransactMode).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListPeVaRuTransactMode.getAttrs())) {
			model.putAll(findListPeVaRuTransactMode.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final PeVaRuTransactModeDto insertPeVaRuTransactMode) {
		if (StringUtils.isBlank(insertPeVaRuTransactMode.getId())) {
			insertPeVaRuTransactMode.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertPeVaRuTransactMode));

		if (MapUtils.isNotEmpty(insertPeVaRuTransactMode.getAttrs())) {
			model.putAll(insertPeVaRuTransactMode.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final PeVaRuTransactModeDto paginationPeVaRuTransactMode) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationPeVaRuTransactMode).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationPeVaRuTransactMode.getAttrs())) {
			model.putAll(paginationPeVaRuTransactMode.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationPeVaRuTransactMode), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final PeVaRuTransactModeDto readPeVaRuTransactMode) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readPeVaRuTransactMode, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readPeVaRuTransactMode.getAttrs())) {
			model.putAll(readPeVaRuTransactMode.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readPeVaRuTransactMode);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readPeVaRuTransactMode, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final PeVaRuTransactModeDto savePeVaRuTransactMode) {
		final P oldPeVaRuTransactMode = this.findByCodeOrId(mappedClass, savePeVaRuTransactMode);

		if (oldPeVaRuTransactMode == null) {
			this.insertByCode(savePeVaRuTransactMode);

			return this.findByCodeOrId(mappedClass, savePeVaRuTransactMode);
		}

		final int updateCount = this.updateByCode(savePeVaRuTransactMode, oldPeVaRuTransactMode);

		if (updateCount < 1) {
			return oldPeVaRuTransactMode;
		}

		return this.findByCodeOrId(mappedClass, savePeVaRuTransactMode);
	}

	@Override
	public int updateByCode(final PeVaRuTransactModeDto updatePeVaRuTransactMode) {
		final PeVaRuTransactModeDto oldPeVaRuTransactMode = this.findByCodeOrId(PeVaRuTransactModeDto.class, updatePeVaRuTransactMode);

		return this.updateByCode(updatePeVaRuTransactMode, oldPeVaRuTransactMode);
	}

	private <P> int updateByCode(final PeVaRuTransactModeDto updatePeVaRuTransactMode, final P oldPeVaRuTransactMode) {
		if (oldPeVaRuTransactMode == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updatePeVaRuTransactMode);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updatePeVaRuTransactMode, oldPeVaRuTransactMode, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldPeVaRuTransactMode, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldPeVaRuTransactMode, updatePeVaRuTransactMode, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updatePeVaRuTransactMode, updateProperties), new ModelArgs(updatePeVaRuTransactMode, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updatePeVaRuTransactMode.getAttrs())) {
			model.putAll(updatePeVaRuTransactMode.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}